package com.swz.dcrm.ui.monthly;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.MonthlyStructure;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnalyzeViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class MonthlyStructureFragment extends BaseFragment {


    @BindView(R.id.pie_chart1)
    PieChart pieChart1;
    @BindView(R.id.pie_chart2)
    PieChart pieChart2;
    @BindView(R.id.pie_chart3)
    PieChart pieChart3;



    @Inject
    AnalyzeViewModel analyzeViewModel;

    private int[] mColors=new int[]{
      R.color.sale_color_rate1,R.color.sale_color_rate2,R.color.sale_color_rate3,R.color.sale_color_rate4,
            R.color.sale_color_rate5,R.color.sale_color_rate6
    };
    public static MonthlyStructureFragment newInstance() {

        return new MonthlyStructureFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        analyzeViewModel.getMonthlyStructure().observe(this, observer);

    }
    Observer observer= (Observer<BaseResponse<MonthlyStructure>>) monthlyStructureBaseResponse -> {
        if(monthlyStructureBaseResponse.isSuccess()){
            MonthlyStructure structure=monthlyStructureBaseResponse.getData();
            int [] data=new int[structure.getMonthNewAddList().size()];
            int [] color=new int[structure.getMonthNewAddList().size()];
            for(int i=0;i<structure.getMonthNewAddList().size();i++){
                data[i]=structure.getMonthNewAddList().get(i).getCount();
                color[i]=getResources().getColor(mColors[i]);
            }
            List<LegendEntry>legendEntries=new ArrayList<>();
            for(int i=0;i<data.length;i++){
                LegendEntry legendEntry=new LegendEntry();
                legendEntry.label=structure.getMonthNewAddList().get(i).getCarModelName()+"  "+structure.getMonthNewAddList().get(i).getCount();
                legendEntry.formColor=getResources().getColor(mColors[i]);
                legendEntry.formSize=12;
                legendEntries.add(legendEntry);
            }
            initPieChart(pieChart1,data,color,legendEntries);
            int [] data1=new int[structure.getMonthOrderCarList().size()];
            int [] color1=new int[structure.getMonthOrderCarList().size()];
            for(int i=0;i<structure.getMonthOrderCarList().size();i++){
                data1[i]=structure.getMonthOrderCarList().get(i).getCount();
                color1[i]=getResources().getColor(mColors[i]);
            }
            List<LegendEntry>legendEntries1=new ArrayList<>();
            for(int i=0;i<data1.length;i++){
                LegendEntry legendEntry=new LegendEntry();
                legendEntry.label=structure.getMonthOrderCarList().get(i).getCarModelName()+"  "+structure.getMonthOrderCarList().get(i).getCount();
                legendEntry.formColor=getResources().getColor(mColors[i]);
                legendEntry.formSize=12;
                legendEntries1.add(legendEntry);
            }
            initPieChart(pieChart2,data1,color1,legendEntries1);

            int [] data2=new int[structure.getMonthDeliveryCarList().size()];
            int [] color2=new int[structure.getMonthDeliveryCarList().size()];
            for(int i=0;i<structure.getMonthDeliveryCarList().size();i++){
                data2[i]=structure.getMonthDeliveryCarList().get(i).getCount();
                color2[i]=getResources().getColor(mColors[i]);
            }
            List<LegendEntry>legendEntries2=new ArrayList<>();
            for(int i=0;i<data2.length;i++){
                LegendEntry legendEntry=new LegendEntry();
                legendEntry.label=structure.getMonthDeliveryCarList().get(i).getCarModelName()+"  "+structure.getMonthDeliveryCarList().get(i).getCount();
                legendEntry.formColor=getResources().getColor(mColors[i]);
                legendEntry.formSize=12;
                legendEntries2.add(legendEntry);
            }
            initPieChart(pieChart3,data2,color2,legendEntries2);

        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_monthly_structure;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        return false;
    }

    public void initPieChart(PieChart pieChart, int[] data,int[]colors,List<LegendEntry>legendEntries) {
        if(data.length==0){
            pieChart.setNoDataText(("暂无此类型数据"));
            pieChart.invalidate();
        }
        Description description = new Description();
//        description.setText("需要展示的内容");
        description.setEnabled(false);
        pieChart.setDescription(description);

        pieChart.setDrawHoleEnabled(false);
        // 百分比显示
        pieChart.setUsePercentValues(true);
        // 禁用触摸
        pieChart.setTouchEnabled(false);
        //设置内置图例不显示

        // 设置隐藏饼图上文字，只显示百分比
        pieChart.setDrawSliceText(false);

        Legend legend=pieChart.getLegend();
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setCustom(legendEntries);

        legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);

        legend.setWordWrapEnabled(true);
        //legend.setXEntrySpace(1000f);

        //数值
        ArrayList<PieEntry> values = new ArrayList<PieEntry>();


        for (int i = 0; i < data.length; i++) {
            //添加数值
            values.add(new PieEntry(data[i], i));
            //添加数值描述
            PieDataSet pieDataSet = new PieDataSet(values, "");
            pieDataSet.setValueTextColor(Color.WHITE);
            pieDataSet.setValueTextSize(12f);
            //将随机的颜色设置进来
            pieDataSet.setColors(colors);
            PieData pieData = new PieData();
            pieData.setDataSet(pieDataSet);
            //设置百分比显示
            pieData.setDrawValues(false);
            //set data...
            pieChart.setData(pieData);
            pieChart.invalidate();
        }
    }
    @OnClick({R.id.tv_new,R.id.tv_order,R.id.tv_delivery_car})
    public void onclick(View view){
        TableFragmentArgs.Builder builder=new TableFragmentArgs.Builder();
        switch (view.getId()){
            case R.id.tv_new:
                builder.setTitle(getString(R.string.monthly_structure1));
                break;
            case R.id.tv_order:
                builder.setTitle(getString(R.string.monthly_structure2));
                break;
            case R.id.tv_delivery_car:
                builder.setTitle(getString(R.string.monthly_structure3));
                break;
        }
        go(null,R.id.action_monthlyFragment_to_tableFragment,builder.build().toBundle());
    }
}
